var  passengerIndex = 0;
var passengers = [];
addPassengerDiv(passengerIndex);
function addPassengerDiv(index) {
    let tpl = `<div id="${"addDiv"+index}" class="user">
        <div class="form-group row">
            <div class="col-sm-6 mb-3 mb-sm-0">
                <input id="${"passengerID"+index}" name="passengerID" type="text" class="form-control form-control-user" placeholder="身份证号码...">
            </div>
            <div class="col-sm-6">
                <input id="${"passengerName"+index}" name="passengerName" type="text" class="form-control form-control-user" placeholder="姓名" >
            </div>
        </div>
        <div class="form-group row">
            <div class="col-sm-6 mb-3 mb-sm-0">
                <input id="${"phone"+index}" name="phone" type="text" class="form-control form-control-user" placeholder="手机号码">
            </div>
            <div class="col-sm-6">
                <div id="${"sex"+index}" class="form-check form-check-inline">
                    <label class="form-check-label">
                        <input type="radio" name="sex" class="form-check-input" value="0">男
                    </label> &nbsp;&nbsp;
                    <label class="form-check-label">
                        <input type="radio" name="sex" class="form-check-input" value="1">女
                    </label>
                </div>
            </div>
        </div>
        <button id="${"addPassengerBtn"+index}" class="btn-sm btn-primary btn-user btn-block">添加</button>
    </div>
    <hr>`;
    $("#addPassenger").append(tpl);
    $("#addPassengerBtn"+index).click(function () {
        var idTest = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
        var phoneTest = /^1[3|4|5|7|8]\d{9}$/;
        var passengerID = $("#passengerID"+index).val();
        var passengerName = $("#passengerName"+index).val();
        var phone=$("#phone"+index).val();
        var sex = $("#sex"+index+" input[name=\"sex\"]:checked").val();
        if(!idTest.test(passengerID)){
            showMsg("身份证号码格式错误",'alert-warning');
            return;
        }
        if(!phoneTest.test(phone)){
            showMsg("手机号码有误，请重填",'alert-warning');
            return;
        }
        if(!passengerName){
            showMsg("姓名不可为空",'alert-warning');
            return;
        }
        if(!sex){
            showMsg("请选择性别",'alert-warning');
            return;
        }
        passengers.push({"passengerID": passengerID,
                        "passengerName":passengerName,
                        "phone":phone,
                        "sex": sex,
                        "creditStatus":"良好"})
        console.log(passengers);
        $("#addDiv"+index+" input").attr("readonly","readonly");
        $("#addDiv"+index+' input:radio').attr('onClick',"javascript:return false");
        $("#addDiv"+index+" button").remove();
        passengerIndex+=1;
        if(passengerIndex<5){
            addPassengerDiv(passengerIndex);
        }else {
            showMsg("最多可添加5位",'alert-warning');
        }
    });
}
$("#submitAdd").click(function () {
    var postdata = JSON.stringify({"passengers":passengers, "ticketTypeID":ticketTypeID});
    console.log(postdata);
    $.ajax({
        url: ctxPath + "book",
        type: 'post',
        data: postdata,
        dataType: 'json',
        contentType : 'application/json',
        success: function (data) {
            if (data["result"] == 1) {
                showMsg(data["msg"], 'alert-success');
                window.location=ctxPath+data["next"];
            }else {
                showMsg("添加乘机人失败！", 'alert-danger')
            }
        }
    });
})

// 显示提示框
function showMsg(msg, style, time) {
    style = (style === undefined) ? 'alert-success' : style;
    time = (time === undefined) ? 1200 : time;
    $('.alert').html(msg).addClass(style).show().delay(time).fadeOut();
}